package com.lanren.familybookapp.ui.user.model;

import com.lanren.common.app.ApiUrl;
import com.lanren.common.base.impl.BaseModel;
import com.lanren.common.http.HttpManager;
import com.lanren.common.utils.GsonUtils;
import com.lanren.common.utils.LogUtils;
import com.lanren.common.utils.md5.MD5Utils;
import com.lanren.easyhttp.callback.SimpleCallBack;
import com.lanren.easyhttp.exception.ApiException;
import com.lanren.familybookapp.app.AppUserHelper;
import com.lanren.familybookapp.ui.user.interfaces.IRegisterModel;

import java.util.LinkedHashMap;

import io.reactivex.disposables.Disposable;

/**
 * @ClassName RegisterModel
 * @Description TODO
 * @Author chongheng.wang
 * @Date 2019/10/5 9:32
 * @Version 1.0
 */
public class RegisterModel extends BaseModel {

    public RegisterModel() {
        initDispoable();
    }

    public void register(final String userName, final String password, final IRegisterModel.RegisterModel
            listener) {

        LinkedHashMap<String, String> map = new LinkedHashMap<>();
        map.put("userId", userName);
        map.put("userPassword", MD5Utils.MD5Encode32(password));

        Disposable disposable = HttpManager.post(ApiUrl.API.USER_REGISTER)
                .headers(AppUserHelper.getHttpHeaders())
                .upJson(GsonUtils.toJson(map))
                .execute(new SimpleCallBack<String>() {
                    @Override
                    public void onError(ApiException e) {
                        LogUtils.d("regist e=" + e.getMessage());
                        listener.onError(e.getMessage());
                    }

                    @Override
                    public void onSuccess(String s) {
                        LogUtils.d("regist=" + GsonUtils.toJson(s));
                        listener.onSuccess();
                    }
                });

        addDisposable(disposable);

    }
}
